/***************************************************************************
 创建者: hualei
 开始时间: 2018-10-20
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2018-10-15 类的具体实现代码编写

 ***************************************************************************
 *  TCP_WRITE 以太网通信数据发送指令具体实现
 ***************************************************************************/
#include "tcpwriteinstructionlogic.h"
#include "programset.h"
#include "programengine.h"
#include "regdata.h"
#include "tcpmanager.h"
#include "tcpwriteinstruction.h"
#include "GeneralDefine.h"
#include "messagelog.h"

extern TcpManager *tcpManager;

TcpWriteInstructionLogic::TcpWriteInstructionLogic(int robotIdIn, ProgramSet *programSetIn, ProgramEngine *programEngineIn, RegData *regDataIn)
{
    robotId = robotIdIn;
    programEngine = programEngineIn;
    programSet = programSetIn;
    regData = regDataIn;

}

E_PROGRAM_LOOP_RESULT TcpWriteInstructionLogic::startTcpWrite(int programNumIn, const TcpWriteInstruction &instructionIn, int lineIndex)
{
<<<<<<< HEAD
    tcpManager->setTcpRobotId(robotId);
=======
    tcpManager->programEngine = programEngine;
    tcpManager->robotId = robotId;
>>>>>>> origin/master

    if(instructionIn.instructionType != TCP_WRITE)
    {
        return E_PROGRAM_LOOP_RESULT_ERROR;
    }

    QString ip = "";
    QByteArray sendData1;
    // 获取要发送的数据
    // 发送常量数据
    if(instructionIn.tcpWriteType == E_TCP_WRITE_TYPE_CONST)
    {
        sendData1 = instructionIn.tcpWriteConstValue.toLatin1();//字符形式发送
    }
    // 发送R寄存器数据
    else if(instructionIn.tcpWriteType == E_TCP_WRITE_TYPE_REGISTER)
    {
        bool initialStatus;
        QVector <double> rRegValueList;

        // 获取R寄存器数据列表
        rRegValueList = regData->returnRValue(initialStatus);
        if(initialStatus)
        {
            for(int i = instructionIn.tcpWriteRegBegAddress; i <= instructionIn.regLengthValue; i++)
            {
                if(i-1>=0 && i-1<rRegValueList.size())
                {
                    sendData1.append(QString::number(rRegValueList[i-1]).toLatin1());
                }
//                sendData1[i] = *QString::number(rRegValueList[instructionIn.tcpWriteRegBegAddress + i]).toLatin1().data();
            }
        }
    }
    // 客户端
    if(instructionIn.tcpWriteBusType == E_TCP_WRITE_BUS_CLIENT)
    {
        for(int i = 0; i < instructionIn.tcpWriteServerIPVec.count(); i++)
        {
            if(i == 3)
            {
                ip += QString::number(instructionIn.tcpWriteServerIPVec.at(i));
            }
            else
            {
                ip += QString::number(instructionIn.tcpWriteServerIPVec.at(i)) + ".";
            }
        }
        // 发送数据，检查是否发送成功
        if(tcpManager->tcpWrite_client(ip, instructionIn.tcpWritePort, sendData1) == -1)
        {
            qDebug() << "TCP_WRITE 指令数据发送失败!";
             QString tmpjumpLabel=QString::number(instructionIn.jmpLBValue);
            return programEngine->startJump(programNumIn,tmpjumpLabel);
//            return E_PROGRAM_LOOP_RESULT_NORMAL;
        }
    }
    // 服务器端
    else if(instructionIn.tcpWriteBusType == E_TCP_WRITE_BUS_SERVER)
    {
        for(int i = 0; i < instructionIn.tcpWriteClientIPVec.count(); i++)
        {
            if(i == 3)
            {
                ip += QString::number(instructionIn.tcpWriteClientIPVec.at(i));
            }
            else
            {
                ip += QString::number(instructionIn.tcpWriteClientIPVec.at(i)) + ".";
            }
        }

        // 发送数据，检查是否发送成功
        if(tcpManager->tcpWrite_server(ip, instructionIn.tcpWritePort, sendData1) != 1)
        {
            qDebug() << "TCP_WRITE 指令数据发送失败!";
            QString tmpjumpLabel=QString::number(instructionIn.jmpLBValue);
            return programEngine->startJump(programNumIn, tmpjumpLabel);
//            return E_PROGRAM_LOOP_RESULT_NORMAL;
        }
    }

    programEngine->calculateNextLineToRun(programNumIn);
    return programEngine->isTheLastInstruction(programNumIn);
}


void TcpWriteInstructionLogic::addMsg(int messageLevel, string componentName, string messageType, int messageCode, int robotId, int parameter1, int parameter2, int parameter3, int parameter4, QString message)
{
    Message tmpMsg;
    tmpMsg.MessageLevel = messageLevel;
    tmpMsg.componentClassName = componentName;
    tmpMsg.messageType = messageType;
    tmpMsg.robotId = robotId;
    tmpMsg.messageCode = messageCode;
   // tmpMsg.time = t1.tv_sec;

    QString infomationStr;

    switch(messageCode)
    {
    case 3904:
    {
       infomationStr = QObject::tr("机器人:") +
               QString::number(robotId) +
               QObject::tr("第") +
               QString::number(parameter1+1) +
               QObject::tr("行运行出现异常（TCP_WRITE tcp发送指令运行错误）！")+message;

        break;
    }
        default:
        {

        }
    }

    tmpMsg.MessageInformation = infomationStr.toStdString();
    MessageLog::getInstance()->addMessage(tmpMsg);
}
